package com.yongninger.activity;

import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yongninger.dao.UserDao;
import com.yongninger.vo.UserVo;

public class LoginActivity extends BaseActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		// 新增按钮点击事件
		Button loginBtn = (Button) this.findViewById(R.id.login);
		SharedPreferences userInfo = getSharedPreferences("user_info", 0);
		String umenu11 = userInfo.getString("uname", "");
		EditText uname = (EditText) this.findViewById(R.id.uname);
		uname.setText(umenu11);
		EditText upwd = (EditText) this.findViewById(R.id.upwd);
		upwd.setText("");
		loginBtn.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if(login()){
						// 跳转到查询页面
						Intent intent = new Intent(LoginActivity.this,
								MainActivity.class);
						startActivity(intent);
					}else{
						Toast.makeText(getApplicationContext(), "用户名或密码错误",
								Toast.LENGTH_SHORT).show();
					}
				}
				return false;
			}

		});
	}

	private boolean login() {
		boolean temp = false;
		EditText uname = (EditText) this.findViewById(R.id.uname);
		EditText upwd = (EditText) this.findViewById(R.id.upwd);
		if (uname.length() <= 0) {
			Toast.makeText(getApplicationContext(), "用户名不能为空",
					Toast.LENGTH_SHORT).show();
			return temp;
		}
		if (upwd.length() <= 0) {
			Toast.makeText(getApplicationContext(), "密码不能为空",
					Toast.LENGTH_SHORT).show();
			return temp;
		}
		UserVo userVo = new UserVo();
		userVo.setUname(uname.getText().toString());
		userVo.setUpwd(upwd.getText().toString());
		UserDao userDao = new UserDao(getApplicationContext());
		List<UserVo> list = userDao.queryUserVoList(userVo,0,0);
		if (list.size() == 1) {
			UserVo tempVo = list.get(0);
			temp = true;
			SharedPreferences userInfo = getSharedPreferences("user_info", 0);
			userInfo.edit().putInt("uid", tempVo.getUid()).commit();
			userInfo.edit().putString("uname", tempVo.getUname()).commit();
			userInfo.edit().putString("upwd", tempVo.getUpwd()).commit();
			userInfo.edit().putString("utype", tempVo.getUtype()).commit();

			userInfo.edit().putString("umenu1", tempVo.getUmenu1()).commit();
			userInfo.edit().putString("umenu11", tempVo.getUmenu11()).commit();
			userInfo.edit().putString("umenu12", tempVo.getUmenu12()).commit();

			userInfo.edit().putString("umenu2", tempVo.getUmenu2()).commit();
			userInfo.edit().putString("umenu21", tempVo.getUmenu21()).commit();
			userInfo.edit().putString("umenu22", tempVo.getUmenu22()).commit();

			userInfo.edit().putString("umenu3", tempVo.getUmenu3()).commit();
			userInfo.edit().putString("umenu31", tempVo.getUmenu31()).commit();
			userInfo.edit().putString("umenu32", tempVo.getUmenu32()).commit();

			userInfo.edit().putString("umenu4", tempVo.getUmenu4()).commit();
			userInfo.edit().putString("umenu41", tempVo.getUmenu41()).commit();
			userInfo.edit().putString("umenu42", tempVo.getUmenu42()).commit();

			userInfo.edit().putString("umenu5", tempVo.getUmenu5()).commit();
			userInfo.edit().putString("umenu51", tempVo.getUmenu51()).commit();
			userInfo.edit().putString("umenu52", tempVo.getUmenu52()).commit();
			
			userInfo.edit().putString("umenu6", tempVo.getUmenu6()).commit();
			userInfo.edit().putString("umenu61", tempVo.getUmenu61()).commit();
			userInfo.edit().putString("umenu62", tempVo.getUmenu62()).commit();
		}
		return temp;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			/*Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);*/
			exit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
